<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class isp_customer_kontrak extends MY_Controller {
	
	function __construct()
	{
		parent::__construct(); 
		
		$this->load->model('isp_customer_kontrak_model', 'IspKontrakP');
		//$this->load->model('isp_combo_model', 'Combo');
		$this->secure_session();
	}
	
	function index(){
		$data['title'] = "Client Kontrak";	
		
		viewByTemplate('isp_customer_kontrak/index', $data, 'grid');
	}

	function id_auto(){
		 $query = $this->db->query("SELECT no_id FROM (SELECT * FROM isp_client_kontrak ORDER BY no_id DESC) WHERE rownum <=1");
	 				if ($query->row_array()) {
	 					$getquery = $query->row_array();
	 					$rowcount = $getquery['no_id'];
	 				} 
		$data['no_id'] = $rowcount;

		echo json_encode($data);
	}

	function id_auto_null(){
		$query = $this->db->query("SELECT no_id FROM (SELECT * FROM isp_client_kontrak ORDER BY no_id DESC) WHERE rownum <=1");
				 				if ($query->row_array()) {
				 					$getquery = $query->row_array();
				 					$rowcount = $getquery['no_id'];
				 					$autonum = $rowcount + 1;
				 				} else {
								$autonum = 1;
							}
		$data['id_kontrak_auto'] = $autonum;
		echo json_encode($data);
	}
	
	function getdata()
	{
		$this->secure_session();
		echo $this->IspKontrakP->GetJSON();
	}

	function getdata2()
	{
		$this->secure_session();
		echo $this->IspKontrakP->GetJSON2();
	}

	function getdata3()
	{
		$this->secure_session();
		echo $this->IspKontrakP->GetJSON3();
	}

	function getdata4()
	{
		$this->secure_session();
		echo $this->IspKontrakP->GetJSON4();
	}

	private function validation()
	{
		$error = new ErrorList();

		// Get Error Template
		$tpl_empty = $this->MessageTemplate("ER001");
		$tpl_duplicate = $this->MessageTemplate("ER002");


		// Validation		
		if (post("kode_kontrak") == '') {
			$error->Add("kode_kontrak", "Kode Kontrak", $tpl_empty);
		
		}

		return $error;
	}
	
	function ajax_post()
	{
		$is_error = 0;
		$ls_response = "";
		$ls_genid = "";

		$error = $this->validation();
		try
		{
			if (count( $error->Get() ) > 0) { $is_error = 1; }
			else
			{
				// Fill Post Data
			   

				$data	=  array(
					'no_id' => $this->input->post('no_id'),
					'id_customer' => $this->input->post('id_customer'),
					'kode_kontrak' => $this->input->post('kode_kontrak'),
					'kode_customer' => $this->input->post('kode_customer'),
					'nama_jenis_barang' => $this->input->post('nama_jenis_barang'),
					'kode_jenis_barang' => $this->input->post('kode_jenis_barang'),
					'tgl_aktifasi' => $this->input->post('tgl_aktifasi'),
					'tgl_awal_kontrak' => $this->input->post('tgl_awal_kontrak'),
					'tgl_akhir_kontrak' => $this->input->post('tgl_akhir_kontrak'),
					'marketing_fee' => $this->input->post('marketing_fee'),
					'harga' => $this->input->post('harga'),
					'keterangan' => $this->input->post('keterangan'),
					);

					$data['tgl_aktifasi']			= ($data['tgl_aktifasi'] == '' ? NULL : date_format3($data['tgl_aktifasi'], 'YYYY-MM-DD'));
					$data['tgl_awal_kontrak']			= ($data['tgl_awal_kontrak'] == '' ? NULL : date_format3($data['tgl_awal_kontrak'], 'YYYY-MM-DD'));
					$data['tgl_akhir_kontrak']			= ($data['tgl_akhir_kontrak'] == '' ? NULL : date_format3($data['tgl_akhir_kontrak'], 'YYYY-MM-DD'));


				
				if (post("mode") == "create")
				{
					//$data['create_date']	= NOW;
					//$data['create_user']	= USERID;

					$this->IspKontrakP->Add($data);

					$ls_response = "Data Tersimpan";
					//App.audit_trail("CS001", "create/" + (data.role_id ?? ""));

				}
				else if (post("mode") == "edit")
				{
					//$data['modify_date']	= NOW;
					//$data['modify_user']	= USERID;

					$cek_data = $this->db->query("select * from ISP_CLIENT_KONTRAK where no_id = '".$this->input->post('no_id')."'");
					if ($cek_data->num_rows() == 0) {//
						$this->IspKontrakP->Add($data);
					} else {
						$this->IspKontrakP->Update($data, array('no_id'	=> $data['no_id']));
					}					

					$ls_response = "Data Tersimpan [Edited]";
					//App.audit_trail("CS002", "edit/" + (data.role_id ?? ""));

				}
			}
		} catch(Exception $e) {
			$is_error = 1; $ls_response = $e->getMessage();
		}
		
		echo $this->PostResponse(json_encode(array(	'is_error'	=> $is_error,
													'fields'	=> $error->GetDictionary(),
													'msg'		=> $ls_response,
													'genid'		=> $ls_genid,
													'form_sender'=> post("form_sender"))
								));
	}

	function ajax_delete()
	{
		$ls_result		= "";
		$ls_response = "";
		$no_id 	= get('no_id');
		
		if ($no_id != "")
		{
			// Delete Single
			$arr_rule	= array('no_id' => $no_id);
			$ls_result = $this->IspKontrakP->DeleteSingle($arr_rule);

		}
		else
		{
			
			$num_row = intval($this->data['num_row']['deleted']);
			
			for ($i = 0; $i < $num_row; $i++)
			{
				$row	= $this->data['deleted'][$i];
				
				$arr_rule	= array('no_id' => $row['no_id']);
				$ls_result .= $this->IspKontrakP->DeleteSingle($arr_rule);
				$ls_response = "Info : Data Customer Kontra Telah Terhapus
				silahkan Update Kembali";
			}
		}
		
		echo $ls_response;
	}
}
?>